import pygame
import math

# 初始化 pygame
pygame.init()

# 屏幕尺寸
WIDTH, HEIGHT = 800, 600
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("太阳、地球和月球动画")

# 颜色
WHITE = (255, 255, 255)
YELLOW = (255, 255, 0)
BLUE = (0, 0, 255)
GRAY = (150, 150, 150)
BLACK = (0, 0, 0)

# 天体参数
SUN_RADIUS = 50       # 太阳半径
EARTH_RADIUS = 20     # 地球半径
MOON_RADIUS = 10      # 月球半径

SUN_POS = (WIDTH // 2, HEIGHT // 2)  # 太阳中心位置

EARTH_ORBIT_RADIUS = 200  # 地球轨道半径
MOON_ORBIT_RADIUS = 50   # 月球轨道半径

EARTH_SPEED = 0.01  # 地球公转速度 (弧度/帧)
MOON_SPEED = 0.05   # 月球公转速度 (弧度/帧)

earth_angle = 0  # 地球初始角度
moon_angle = 0   # 月球初始角度

# 帧率
FPS = 60
clock = pygame.time.Clock()

# 字体设置
font = pygame.font.Font(None, 30)  # 使用默认字体，大小 30. 可以替换字体文件，例如 font = pygame.font.Font("arial.ttf", 30) 如果有arial.ttf文件

# 动画循环
running = True
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

    # 更新地球位置
    earth_x = SUN_POS[0] + EARTH_ORBIT_RADIUS * math.cos(earth_angle)
    earth_y = SUN_POS[1] + EARTH_ORBIT_RADIUS * math.sin(earth_angle)
    earth_pos = (int(earth_x), int(earth_y)) # 转换为整数坐标

    # 更新月球位置 (相对于地球)
    moon_x = earth_pos[0] + MOON_ORBIT_RADIUS * math.cos(moon_angle)
    moon_y = earth_pos[0] + MOON_ORBIT_RADIUS * math.sin(moon_angle)
    moon_pos = (int(moon_x), int(moon_y)) # 转换为整数坐标

    # 增加角度，模拟运动
    earth_angle += EARTH_SPEED
    moon_angle += MOON_SPEED

    # 绘制背景
    screen.fill(BLACK)

    # 绘制太阳
    pygame.draw.circle(screen, YELLOW, SUN_POS, SUN_RADIUS) # 黄色圆圈是太阳
    # 太阳文字
    sun_text_surface = font.render("Sun", True, WHITE) # 创建文字表面
    sun_text_rect = sun_text_surface.get_rect(center=(SUN_POS[0], SUN_POS[1] - SUN_RADIUS - 20)) # 获取文字矩形，并设置中心位置在太阳上方
    screen.blit(sun_text_surface, sun_text_rect) # 绘制太阳文字

    # 绘制地球
    pygame.draw.circle(screen, BLUE, earth_pos, EARTH_RADIUS) # 蓝色圆圈是地球
    # 地球文字
    earth_text_surface = font.render("Earth", True, WHITE) # 创建文字表面
    earth_text_rect = earth_text_surface.get_rect(center=(earth_pos[0], earth_pos[1] - EARTH_RADIUS - 20)) # 获取文字矩形，并设置中心位置在地球上方
    screen.blit(earth_text_surface, earth_text_rect) # 绘制地球文字

    # 绘制月球
    pygame.draw.circle(screen, GRAY, moon_pos, MOON_RADIUS) # 灰色圆圈是月球
    # 月球文字
    moon_text_surface = font.render("Moon", True, WHITE) # 创建文字表面
    moon_text_rect = moon_text_surface.get_rect(center=(moon_pos[0], moon_pos[1] - MOON_RADIUS - 20)) # 获取文字矩形，并设置中心位置在月球上方
    screen.blit(moon_text_surface, moon_text_rect) # 绘制月球文字

    # 更新显示
    pygame.display.flip()

    # 控制帧率
    clock.tick(FPS)

pygame.quit()